#include "inc-all.h"
#include "DemoApp.h"

using namespace std;
using namespace Spears;

void initialization();
void load_assets();
void uninitialization();

namespace demoglobal
{
	std::unique_ptr<Spears::Camera>			g_p_camera;
	std::unique_ptr<Spears::Renderer>		g_p_renderer;
	std::unique_ptr<Spears::Scene>			g_p_scene_main;
	std::unique_ptr<Spears::Application>	g_p_app;
}

int main(int argc, char** argv)
{
	using namespace demoglobal;
	try
	{
		int see = sizeof(Spears::Asset::atMaterial);
		see = sizeof(std::unordered_map<int, void*>);
		see = sizeof(std::map<int, void*>);

		initialization();
		
		dynamic_cast<DemoApp*>(g_p_app.get())->one_frame();

// 		g_p_app->run();
// 		system("pause");
// 		g_p_app->stop();
	}
	catch (std::exception ex)
	{
		cout << ex.what() << endl;
	}

	return 0;
}

void initialization()
{
	using namespace demoglobal;
	try
	{		
		Spears::NaiveRenderer* p_nrdr = new Spears::NaiveRenderer();
		Spears::Texture2D*	p_tex2_rt = new Spears::Texture2D(Spears::Math::Resolution(1000, 800));
		Spears::DefaultCamera* p_dcam = new Spears::DefaultCamera(10.f, 8.f, 4.f);
		Spears::Application* p_app = new DemoApp();

		load_assets();

		p_nrdr->set_render_target(p_tex2_rt);
		p_nrdr->set_camera(p_dcam);
		p_nrdr->set_scene(g_p_scene_main.get());
		
		p_dcam->move_z(-6.0f);
		//p_dcam->move_y(5.0f);
		p_dcam->move_x(-5.0f);

		p_app->set_frames_interval(16.6f);

		g_p_renderer.reset(p_nrdr);
		g_p_camera.reset(p_dcam);
		g_p_app.reset(p_app);

		
	}
	catch (std::exception ex)
	{
		throw ex;
	}

}

void load_assets()
{
	using namespace demoglobal;

	shared_ptr<Asset::atScene>
		p_asset_scene(Asset::Utilities::load_asset("../../../test/outputs/cone.json"));

	vector<Asset::atLight*>& lights = p_asset_scene->lights();
	vector<Asset::atMesh*>& meshes = p_asset_scene->meshes();
	Asset::atNode const* scene_tree = p_asset_scene->scene_tree();

	cout << "Lights Count: " << lights.size() << endl;
	cout << "Meshes Count: " << meshes.size() << endl;

	Spears::CascadingScene* p_scene = new Spears::CascadingScene();
	Spears::Utils::CascadingSceneUtil::load_scene_from_asset(*p_scene,
		Spears::StablePool<Spears::NodeComponent>::get_instance(),
		Spears::StablePool<Spears::SceneNode>::get_instance(),
		Spears::StablePool<Spears::Math::Material>::get_instance(),
		*p_asset_scene);
	p_scene->set_background_color(Math::Color(0.11f, 0.22f, 0.33f));

	g_p_scene_main.reset(p_scene);

}

void uninitialization()
{

}
